<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Category_manual_tournament_entity{
	var $id = 0;
	var $key_id = "";
	var $category_name = "";
	var $status = "";
	var $type = "box"; 
	var $is_open = "yes";
	var $number_boxes = 0;
	var $range_denote = 0;
	var $ordering = 0;
	var $odds = 0;
	var $fix_answer = 0;
	var $correct_answer = "";
	var $tournament_id = 0;
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_CATEGORY_MANUAL_TOURNAMENT)->result();
		}
		else if($cmd = 'key_id'){
			$result = $ci->db->where('key_id',$input)->get(TBL_CATEGORY_MANUAL_TOURNAMENT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->key_id = $result[0]->key_id;
			$this->category_name = $result[0]->category_name;
			$this->status = $result[0]->status;
			$this->type = $result[0]->type;
			$this->is_open = $result[0]->is_open;
			$this->number_boxes = $result[0]->number_boxes;
			$this->range_denote = $result[0]->range_denote;
			$this->ordering = $result[0]->ordering;
			$this->odds = $result[0]->odds;
			$this->fix_answer = $result[0]->fix_answer;
			$this->correct_answer = $result[0]->correct_answer;
			$this->tournament_id = $result[0]->tournament_id;
			
			return $this;
		}
		return false;
	}
	
	function setTournamentId($input){ $this->tournament_id=$input;}
	function setCategoryName($input){ $this->category_name=$input; }
	function setStatus($input){ $this->status=$input; }
	function setType($input){ $this->type=$input; }
	function setIsOpen($input){ $this->is_open=$input; }
	function setNumberBoxes($input){ $this->number_boxes=$input; }
	function setRangeDenote($input){ $this->range_denote=$input; }
	function setOrdering($input){ $this->ordering=$input; }
	function setOdds($input){ $this->odds=$input; }
	function setFixAnswer($input){ $this->fix_answer=$input; }
	function setCorrectAnswer($input){ $this->correct_answer=$input; }
		
	function getId(){ return $this->id; }
	function getCategoryName(){ return $this->category_name; }
	function getTournamentId(){ return $this->tournament_id; }
	function getStatus(){ return $this->status; }
	function getType(){ return $this->type; }
	function getOrdering(){ return $this->ordering; }
	function getIsOpen(){ return $this->is_open; }
	function getKeyId(){ return $this->key_id; }
	function getNumberBoxes(){ return $this->number_boxes; }
	function getRangeDenote(){ return $this->range_denote; }
	function getOdds(){ return $this->odds; }
	function getFixAnswer(){ return $this->fix_answer; }
	function getCorrectAnswer(){ return $this->correct_answer; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('category_name', $this->category_name);
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('status', $this->status);
		$ci->db->set('type', $this->type);
		$ci->db->set('ordering', $this->ordering);
		$ci->db->set('is_open', $this->is_open);
		$ci->db->set('key_id', $this->key_id);
		$ci->db->set('number_boxes', $this->number_boxes);
		$ci->db->set('range_denote', $this->range_denote);
		$ci->db->set('odds', $this->odds);
		$ci->db->set('fix_answer', $this->fix_answer);
		$ci->db->set('correct_answer', $this->correct_answer);
		
		$ci->db->where('id', $this->id)->update(TBL_CATEGORY_MANUAL_TOURNAMENT);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		$ci->db->set('key_id', $this->generate_key_id());
		$ci->db->insert(TBL_CATEGORY_MANUAL_TOURNAMENT);
		
		return $this->init( 'id', $ci->db->insert_id() );
	}
	
	function getNextOrdering($tournament_id){
		$ci=& get_instance();
		$count = count( array_filter( $ci->db->where('tournament_id', $tournament_id)->get(TBL_CATEGORY_MANUAL_TOURNAMENT)->result() ) );
		return $count+1;
	}
	
	private function generate_key_id(){
		$ci=& get_instance();
		$records = $ci->db->get(TBL_CATEGORY_MANUAL_TOURNAMENT)->result();
		$numberArray = array();
		foreach($records as $obj){
			$numberArray[] = intval(substr($obj->key_id,2,strlen($obj->key_id)));
		}
		while(true){
			$rand = "MT".$ci->digit->rand_digit(9);
			if(!in_array($rand, $numberArray)){
				return $rand;
			}
		}
	}
	
	function getListCategoriesByStatus($tournament_id, $status='living'){
		$ci=& get_instance();
		return $ci->db->where( array('tournament_id'=> $tournament_id, 'status'=>$status) )->order_by('ordering','desc')->get(TBL_CATEGORY_MANUAL_TOURNAMENT)->result();
	}
	
	function getOpenListCategories($tournament_id){ // frontend
		$ci=& get_instance();
		return $ci->db->where( array('tournament_id'=> $tournament_id, 'is_open'=>'yes', 'status'=>'living') )->order_by('ordering','desc')->get(TBL_CATEGORY_MANUAL_TOURNAMENT)->result();
	}
	
	function getLivingCategoriesOfMatch($tournament_id){
		$ci=& get_instance();
		return $ci->db->where(array('tournament_id'=>$tournament_id, 'status'=>'living'))->get(TBL_CATEGORY_MANUAL_TOURNAMENT)->result();
	}
	
	
	
	
	
	//end class
}
